package weather.gui.panels;
import java.awt.*;

import java.awt.Dimension;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.JScrollPane;
import javax.swing.WindowConstants;
import javax.swing.JFrame;

import weather.gui.panels.utils.ExtendedTextPanel;
import weather.language.ForecastEngine;

@SuppressWarnings("serial")
public class ChatPanel extends javax.swing.JPanel {
	private static final int MSG_HEADER_WIDTH = 45;

	private ExtendedTextPanel textChatPanel;
	private ForecastEngine fe = new ForecastEngine();

	private String userName = "User";
	private String prefix = "  ";
	private String welcome = "     Welcome to the JMWeather. Please ask me any\n" +
							 "     question about the weather, maybe I can help you";
	
	private String msgHeaderFontFamily = new String("Monospaced");
	private String msgContentFontFamily = new String("Arial");
	
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new ChatPanel());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public ChatPanel() {
		super();
		String userName = System.getProperty("user.name");
		if(userName!=null && !userName.equals(""))
			this.userName = userName;
		initGUI();
		textChatPanel.append(Color.BLUE, alignMsgHeader("JMWeather", getTime(), MSG_HEADER_WIDTH), msgHeaderFontFamily, true, 14, Color.lightGray);
		textChatPanel.append(Color.BLACK, welcome+"\n", msgContentFontFamily, false, 12, Color.white);
	}
	
	private void initGUI() {
		try {
			BorderLayout thisLayout = new BorderLayout();
			this.setLayout(thisLayout);
			setPreferredSize(new Dimension(400, 300));
			{
				textChatPanel = new ExtendedTextPanel();
				this.add(new JScrollPane(textChatPanel), BorderLayout.CENTER);
				textChatPanel.setEditable(false);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * Call-back function. It is called as long as question has been wrote 
	 * by user. This is the only method from view which should communicate with
	 * JMWeather Engine.
	 * @param question	question in natural language, specified by user
	 */
	public void onQuestion(String question)
	{
		textChatPanel.append(Color.YELLOW, alignMsgHeader(userName, getTime(), MSG_HEADER_WIDTH), msgHeaderFontFamily, true, 14, Color.GRAY);
		
		textChatPanel.append(Color.BLACK, prefix+question+"\n", msgContentFontFamily, false, 12, Color.white);
		
		String answer = "I don't know";
		//TODO: Communicate with Engine
		answer = fe.getForecast(question);
		
		textChatPanel.append(Color.BLUE, alignMsgHeader("JMWeather", getTime(), MSG_HEADER_WIDTH), msgHeaderFontFamily, true, 14, Color.lightGray);
		textChatPanel.append(Color.BLACK, prefix+answer+"\n", msgContentFontFamily, false, 12, Color.white);
		
	}

	private String alignMsgHeader(String userName, String time, int len)
	{
		StringBuffer aligned = new StringBuffer();
		int n = len - userName.length() - time.length();
		
		aligned.append(prefix);
		aligned.append(userName);
		do
			aligned.append(' ');
		while (n-- > 0);
		aligned.append(time);
		aligned.append("   \n");
		
		return new String(aligned);
	}
	
	private String getTime()
	{
		Calendar cal = Calendar.getInstance();
	    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss   dd-MM-yyyy");
	    String currentDate = sdf.format(cal.getTime());
	    
	    return currentDate;
	}

}
